// tbkPackage/pages/comprehensive/comprehensive.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        goBack: 1,
        showLoading: 1,
        leader: 0,
        allBottom: 0,
        //2为加载完  1为加载中  0为还有
        swiper: {
            indicatorDots: false,
            vertical: false,
            autoplay: true,
            interval: 2e3,
            duration: 500
        },
        jpPage: 1,
        jpPageSize: 10,
        jpGoods: [],
        //京东淘宝混合商品
        jd: 0,
        pd: 0,
        jd_hot: 0,
        //京东精选
        pd_kill: 0,
        //拼多多秒杀
        pd_hot: 0,
        //拼多多今日爆款
        pd_empty: 0
    },
    //获取去右侧胶囊的信息
    getHeader() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        this.setData({
            topBarHeight: menuRect.height,
            topBarTop: menuRect.top
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getHeader();
        let that = this;
        let pagesLength = getCurrentPages().length;
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX,
            pagesLength: pagesLength
        });
        that.setData({
            domain: app.globalData.domain.slice(0, -10),
            domainJdSmall: app.globalData.domain.slice(0, -10) + "img/tbkPackage/jd_small.png",
            domainPdSmall: app.globalData.domain.slice(0, -10) + "img/tbkPackage/pd_small.png",
            domainJdBig: app.globalData.domain.slice(0, -10) + "img/tbkPackage/jd_big.png",
            domainPdBig: app.globalData.domain.slice(0, -10) + "img/tbkPackage/pd_big.png"
        });
        that.getBase();
        //基础配置
                that.getMyLeaderInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getJpGoods();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        return {
            title: "",
            path: "/tbkPackage/pages/comprehensive/comprehensive"
        };
    },
    //幻灯片切换
    swiperChange: function(e) {
        this.setData({
            currentIndex: e.detail.current
        });
    },
    //获取团长信息
    getMyLeaderInfo: function() {
        var that = this;
        var user = wx.getStorageSync("user");
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: user.openid
            },
            success: res => {
                //判断哪个团长优先
                //1.我是团长，则优先用自己
                //2.本次分享团长 优先级 大于 历史绑定团长
                if (res.extra.iam_leader) {
                    var leader = res.extra.leader_id;
                } else {
                    var leader = that.data.leader != "" ? that.data.leader : res.extra.leader_id;
                }
                let commissionname = res.extra.iam_leader == 1 ? "返佣" : "返利";
                that.setData({
                    iam_leader: res.extra.iam_leader,
                    leader: leader,
                    commissionname: commissionname
                });
                //获取佣金比例
                                common.request({
                    endpoint: "Taoke/common/brokerage-setting-view",
                    method: "post",
                    data: {},
                    success: res => {
                        //根据身份，判断显示的佣金或返利比例
                        if (that.data.iam_leader) {
                            var commission = res.extra.is_taoke_leader_brokerage == 1 ? res.extra.taoke_leader_brokerage_rate : 0;
                        } else {
                            var commission = res.extra.is_taoke_user_brokerage == 1 ? res.extra.taoke_user_brokerage_rate : 0;
                        }
                        var st = res.extra.st;
                        that.setData({
                            st: st
                        });
                        if (st == "1") {
                            //开通拼多多
                            that.setData({
                                pd: 1,
                                pd_hot: 1
                            });
                            that.getPdHotGoods(1);
                            that.getPdHotGoods(2);
                            that.getPdHotGoods(3);
                            that.getJpGoods();
                            //京东拼多多混合
                                                } else if (st == "2") {
                            //开通京东
                            that.getJdHotGoods();
                            //京东精选
                                                        that.setData({
                                jd: 1,
                                jd_hot: 1
                            });
                            that.getJpGoods();
                            //京东拼多多混合
                                                } else if (st == "1,2") {
                            //2个都开通
                            that.setData({
                                pd: 1,
                                pd_hot: 1,
                                jd: 1,
                                jd_hot: 1
                            });
                            that.getPdHotGoods(1);
                            that.getPdHotGoods(2);
                            that.getPdHotGoods(3);
                            that.getJdHotGoods();
                            //京东精选
                                                        that.getJpGoods();
                            //京东拼多多混合
                                                }
                        that.setData({
                            commission: commission,
                            commissionUser: res.extra.is_taoke_user_brokerage
                        });
                    }
                });
            }
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    is_open: res.extra.is_open,
                    copyImg: res.extra.copyright_img,
                    time_s: res.extra.time_s,
                    time_e: res.extra.time_e,
                    phone: res.extra.phone,
                    orderlist: res.extra.orderlist,
                    sleep_img: res.extra.sleep_img,
                    is_mcity: res.extra.is_mcity,
                    rp_name: res.extra.rp_name,
                    share_index: res.extra.share_index,
                    //首页分享图链接
                    share_title: res.extra.share_title ? res.extra.share_title : res.extra.title,
                    //首页分享标题
                    if_search_index: res.extra.if_search_index,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    lock_fans: res.extra.lock_fans,
                    leader_style: res.extra.leader_style
                });
            }
        });
    },
    //京东商品
    getJinCate: function() {
        var that = this;
        common.request({
            endpoint: "Taoke/common/jin-cate",
            method: "post",
            data: {
                page: 1,
                page_size: 10
            },
            success: res => {
                that.setData({
                    jinCateGoods: res.extra.data
                });
            }
        });
    },
    //拼多多商品
    getPinCate: function() {
        var that = this;
        common.request({
            endpoint: "Taoke/common/pin-cate",
            method: "post",
            data: {
                page: 1,
                page_size: 10
            },
            success: res => {
                that.setData({
                    pinCateGoods: res.extra.goods_search_response.goods_list
                });
            }
        });
    },
    //京东拼多多混合
    getJpGoods: function() {
        var that = this;
        common.request({
            endpoint: "Taoke/common/jp-goods",
            method: "post",
            data: {
                page: that.data.jpPage,
                page_size: that.data.jpPageSize
            },
            success: res => {
                if (res.extra.generate && res.extra.generate == 1) {
                    //跳转到拼多多
                    wx.navigateToMiniProgram({
                        appId: res.extra.rp_promotion_url_generate_response.url_list[0].we_app_info.app_id,
                        path: res.extra.rp_promotion_url_generate_response.url_list[0].we_app_info.page_path,
                        success(res) {
                            console.log("navigate success");
                        }
                    });
                } else {
                    if (res.extra.goods_search_response) {
                        if (res.extra.goods_search_response.goods_list.length < 1) {
                            that.setData({
                                showLoading: 0
                            });
                        } else {
                            let jpGoods = that.data.jpPage == 1 ? res.extra.goods_search_response.goods_list : that.data.jpGoods.concat(res.extra.goods_search_response.goods_list);
                            that.setData({
                                jpGoods: jpGoods,
                                jpPage: that.data.jpPage + 1
                            });
                        }
                    }
                    if (res.extra.data) {
                        if (res.extra.data.length < 1) {
                            that.setData({
                                showLoading: 0
                            });
                        } else {
                            let jpGoods = that.data.jpPage == 1 ? res.extra.data : that.data.jpGoods.concat(res.extra.data);
                            that.setData({
                                jpGoods: jpGoods,
                                jpPage: that.data.jpPage + 1
                            });
                        }
                    }
                }
            }
        });
    },
    //拼多多爆款,清仓，秒杀
    getPdHotGoods: function(value) {
        var that = this;
        common.request({
            endpoint: "Taoke/pinduoduo/get-product-info",
            method: "post",
            data: {
                page: 1,
                page_size: 10,
                type: "hot",
                value: value
            },
            success: res => {
                switch (value) {
                  case 1:
                    that.setData({
                        //爆款
                        pd_hot: res.extra.goods_basic_detail_response.list.length < 1 ? "0" : "1",
                        pdHotGoods: res.extra.goods_basic_detail_response.list
                    });
                    break;

                  case 2:
                    that.setData({
                        //品牌清仓
                        pd_empty: res.extra.goods_basic_detail_response.list.length < 1 ? "0" : "1",
                        pdEmptyGoods: res.extra.goods_basic_detail_response.list
                    });
                    break;

                  case 3:
                    that.setData({
                        //秒杀
                        pd_kill: res.extra.top_goods_list_get_response.list.length < 1 ? "0" : "1",
                        pdKillGoods: res.extra.top_goods_list_get_response.list
                    });
                    break;
                }
            }
        });
    },
    //京东爆款
    getJdHotGoods: function() {
        var that = this;
        common.request({
            endpoint: "Taoke/jingdong/get-product-info",
            method: "post",
            data: {
                page: 1,
                page_size: 10,
                type: "hot",
                value: "1"
            },
            success: res => {
                if (res.extra.data.length < 1) {
                    that.setData({
                        jd_hot: 0
                    });
                }
                that.setData({
                    jdHotGoods: res.extra.data
                });
            }
        });
    },
    goToDetail: function(e) {
        wx.navigateTo({
            url: "/tbkPackage/pages/detail/detail?channel=" + e.currentTarget.dataset.channelename + "&id=" + e.currentTarget.dataset.id + "&leader=" + this.data.leader
        });
    },
    goToProlist: function(e) {
        wx.navigateTo({
            url: "/tbkPackage/pages/proList/proList?" + e.currentTarget.dataset.item
        });
    },
    goToProlistSearch: function(e) {
        let that = this;
        wx.navigateTo({
            url: "/tbkPackage/pages/proListSearch/proListSearch?" + e.currentTarget.dataset.item + "&st=" + that.data.st
        });
    }
});